package kr.co.mustgo.model.dao.noticeboard;

import java.util.HashMap;
import java.util.List;

import kr.co.mustgo.domain.noticeboard.NoticeBoard;
import kr.co.mustgo.util.BoardUtilities;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class NoticeBoardDAOImpl implements NoticeBoardDAO {

	@Autowired
	private SqlSessionTemplate session;
	@Override
	public int insertContentForNewCount(NoticeBoard noticeBoard) {
		return session.insert(makeSqlId("insertContentForNewCount"), noticeBoard);
	}
	
	@Override
	public int updateViewCount(int code) {
		return session.update(makeSqlId("updateViewCount"), code);

	}
	@Override
	public NoticeBoard selectContentByCode(int code){
		return session.selectOne(makeSqlId("selectContentByCode"), code);
	}
	
	@Override
	public List<NoticeBoard> selectNoticeBoardList(int page){
		HashMap map = new HashMap();
		map.put("contentPerPage", BoardUtilities.CONTENT_PER_PAGE);
		map.put("page", page);
		return session.selectList(makeSqlId("selectNoticeBoardList"), map);
	}

	@Override
	public int selectTotalContent() {
		return session.selectOne(makeSqlId("selectTotalContent"));
	}

	@Override
	public int updateContent(NoticeBoard noticeBoard) {
		return session.update(makeSqlId("updateContent"), noticeBoard);
	}

	@Override
	public int deleteContentByCode(int code) {
		return session.delete(makeSqlId("deleteContentByCode"), code);
	}
	
	private String makeSqlId(String code) {
		return "noticeBoard." + code;
	}

}
